// pages/order_check_query/order_check_query.ts
export { };
const urls = require("../../utils/urls");
const my_date = require("../../utils/my_date");
import drawQrcode from "../../utils/gen_qrcode";


class Order {
    "uuid": string;
    "group_check_uuid": string;
    "user_uuid": string;
    "state": string;
    // 首签到时间
    "sign_in_time": string;
    "order_time": string;
    // show data
    "sign_in_time_show": string;
    "state_show": string;
};

const get_state_show = (state: string) => {
    if (state == "0") {
        return "已预约未检测";
    }
    else if (state == "1") {
        return "已检测";
    }
    else if (state == "2") {
        return "预约失败";
    }
    return "error";
}

Page({

    /**
     * 页面的初始数据
     */
    data: {
        "group_check_orders": ([] as Order[]),
        "show_task_scan_code": false,
        "task_scan_code_image": "/static/order_check.png"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.query_orders();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    query_orders() {
        let that = this;
        const app_user_info = wx.getStorageSync("app_user_info");
        wx.request({
            url: urls.gc_query_order_history_url,
            data: {
                "user_uuid": app_user_info.uuid
            },
            success(res: any) {
                console.log("request get_group_check_task_by_uuid");
                console.log(res);
                let orders = res.data.orders;
                for (let [i, v] of orders.entries()) {
                    v["sign_in_time_show"] = my_date.get_readable_date(v["sign_in_time"]);
                    v["state_show"] = get_state_show(v["state"] || "0");
                    v["index"] = i;
                }
                that.setData({
                    group_check_orders: orders,
                });
            }
        })
    },
    display_task_scan_code(e: any) {
        const index = e.currentTarget.dataset.group_check_order_index;
        const group_check_order = this.data.group_check_orders[index];
        this.setData({
            show_task_scan_code: true
        });
        this.gen_group_check_qrcode(group_check_order);
    },
    back_to_root() {
        wx.navigateBack({ delta: 10 });
    },
    gen_group_check_qrcode(group_check_order: Order) {
        console.log("gen_group_check_qrcode");
        console.log(group_check_order);
        const order_uuid = group_check_order.uuid;
        console.log(order_uuid);
        const query = wx.createSelectorQuery()
        query.select('#task_scan_code_canvas')
            .fields({
                node: true,
                size: true
            })
            .exec((res) => {
                var canvas = res[0].node
                // 调用方法drawQrcode生成二维码
                drawQrcode({
                    canvas: canvas,
                    canvasId: 'task_scan_code_canvas',
                    padding: 30,
                    background: '#ffffff',
                    foreground: '#000000',
                    text: order_uuid,
                })
            })
    }
})